from django.utils.deprecation import MiddlewareMixin
# from django.middleware.common import MiddlewareMixin
import json
from django.http import HttpResponse
from .utils.utils import *


class CrossDomainMiddle(MiddlewareMixin):

    def process_response(self, request, response):
        # 简单请求
        response['Access-Control-Allow-Origin'] = '*'  # 允许所有客户端
        # 非简单请求
        if request.method == 'OPTIONS':
            response['Access-Control-Allow-Methods'] = '*'
            response['Access-Control-Allow-Headers'] = '*'
        return response


class MD1(MiddlewareMixin):
    def process_request(self, request):
        print("MD1里面的 process_request")

    def process_response(self, request, response):
        print("MD1里面的 process_response")
        return response


# 全局错误处理
class ExceptionMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        return fail(str(exception))
